<?xml version="1.0" encoding="UTF-8" ?>
<?arg urn:xmd-environment=Eldorado.Metadata.XmdEnvironment, Eldorado.Metadata ?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:typing="urn:xmd-typing"
	xmlns:mapping="urn:xmd-mapping"
	xmlns:log="urn:log"
	xmlns:env="urn:xmd-environment">

  <xsl:output method="text"/>

  <DBInput/>

  <xsl:template name="GetClassUUID">
    <xsl:param name="className"/>
    <xsl:choose>
      <UUIDInput/>
      <xsl:otherwise>
        <xsl:message terminate="yes">
          uuidd for class '<xsl:value-of select="$className"/>' not defined
        </xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="/">---- File: Delete.Satori.<xsl:value-of select="local-name(/*[1])"/>.sql
    <xsl:variable name="entities" select="*/*[typing:entity[not(@abstract='true')]]"/>
    <xsl:variable name="references" select="//typing:reference"/>
    <xsl:variable name="SetTotal" select="env:SetProgressTotal(count($entities) * 3)" xmlns:env="urn:xmd-environment" />
    <xsl:apply-templates select="$entities" mode="delete"/>
    <xsl:apply-templates select="$references" mode="delete"/>

---- File: Create.Satori.<xsl:value-of select="local-name(/*[1])"/>.sql

    CREATE TABLE if not exists SatoriTables(
    class varchar(255) not null,
    classId int not null,
    prefix varchar(10) not null default '',
    PRIMARY KEY (class, classId)
    ,Key ClassId (ClassId)
    ,Key Class (Class)
    ,key Prefix (Prefix)
    ) ENGINE=InnoDB;
   

    <xsl:apply-templates  select="$entities" mode="create"/>
    <xsl:apply-templates select="$references" mode="create"/>

    <xsl:apply-templates select="$entities" mode="insert"/>

---- File: Index.Satori.<xsl:value-of select="local-name(/*[1])"/>.sql
    <xsl:apply-templates  select="$entities" mode="index"/>
  </xsl:template>

  <xsl:template match="*[typing:entity]" mode="delete">
    drop table if exists `<xsl:value-of select="env:TypeName(typing:entity/@type)"/>`
  </xsl:template>

  <xsl:template match="*[typing:entity]" mode="create">

    <xsl:variable name="className" select="env:TypeName(typing:entity/@type)" xmlns:env="urn:xmd-environment"/>
    <xsl:variable name="classId">
      <xsl:call-template name="GetClassUUID">
        <xsl:with-param name="className" select="$className"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="log" select="env:Log(concat('Processing type ', className),'0')" xmlns:env="urn:xmd-environment"/>
    <xsl:variable name="progress" select="env:UpdateProgress()" xmlns:env="urn:xmd-environment"/>
    <!--<xsl:message>entity <xsl:value-of select="className"/></xsl:message>-->

    <xsl:if test="not(typing:entity/@abstract='true')">
      
      CREATE TABLE if not exists `<xsl:value-of select="$className"/>` (
      `OID` char(36) NOT NULL default '00000000-0000-0000-0000-000000000000'<xsl:for-each select="*[typing:property | typing:reference]">
        <xsl:apply-templates select="." mode="column"/>
      </xsl:for-each>
      ,`modifiele` datetime not null default '1800-01-01T00:00:00'
      ,`modifiepar` varchar(30) not null default 'sql'
      ,`TableName` varchar(50) not null default '<xsl:value-of select="$className"/>'
      ,PRIMARY KEY  (`OID`)
      <xsl:if test="count(cle) + count(Cle) = 1">,key `cle` (`cle`)</xsl:if>
      ) ENGINE=InnoDB;

    </xsl:if>
  </xsl:template>

  <xsl:template match="*[typing:entity]" mode="delete">
    <xsl:variable name="className" select="env:TypeName(typing:entity/@type)" xmlns:env="urn:xmd-environment"/>
    <xsl:variable name="classId">
      <xsl:call-template name="GetClassUUID">
        <xsl:with-param name="className" select="$className"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="log" select="env:Log(concat('Processing type ', className),'0')" xmlns:env="urn:xmd-environment"/>
    <xsl:variable name="progress" select="env:UpdateProgress()" xmlns:env="urn:xmd-environment"/>
    <!--<xsl:message>entity <xsl:value-of select="className"/></xsl:message>-->

    <xsl:if test="not(typing:entity/@abstract='true')">
      drop table if exists `<xsl:value-of select="$className"/>`;

    </xsl:if>
  </xsl:template>

  <xsl:template match="*[typing:entity]" mode="index">
    <xsl:variable name="className" select="env:TypeName(typing:entity/@type)" xmlns:env="urn:xmd-environment"/>
    <xsl:variable name="classId">
      <xsl:call-template name="GetClassUUID">
        <xsl:with-param name="className" select="$className"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="not(typing:entity/@abstract='true')">
      <xsl:for-each select="*[typing:property | typing:reference]">
        <xsl:apply-templates select="." mode="indexcolumn">
          <xsl:with-param name="classId" select="$classId"/>
        </xsl:apply-templates>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>

  <xsl:template match="*[typing:entity]" mode="insert">
    <xsl:variable name="className" select="env:TypeName(typing:entity/@type)" xmlns:env="urn:xmd-environment"/>
    <xsl:variable name="classId">
      <xsl:call-template name="GetClassUUID">
        <xsl:with-param name="className" select="$className"/>
      </xsl:call-template>
    </xsl:variable>

    REPLACE into SatoriTables (class, classId) values ('<xsl:value-of select="$className"/>', <xsl:value-of select="$classId"/>);    

  </xsl:template>

  <xsl:template match="typing:property" mode="column">
    <xsl:variable name="className" select="env:TypeName(../../typing:entity/@type)" xmlns:env="urn:xmd-environment"/>
    <xsl:variable name="classId">
      <xsl:call-template name="GetClassUUID">
        <xsl:with-param name="className" select="$className"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="type">
      <xsl:apply-templates select="@type">
        <xsl:with-param name="classId" select="$classId"/>
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:variable name="name">
      <xsl:value-of select="local-name(..)"/>
    </xsl:variable>
    ,`<xsl:value-of select="$name"/>` <xsl:choose>
      <xsl:when test="$name = 'cle'"> INT not null auto_increment</xsl:when>
      <xsl:when test="$name = 'Cle'"> INT not null auto_increment</xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$type"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="typing:reference[@multiplicity = '1']" mode="column">
    <xsl:variable name="back-role">
      <xsl:value-of select="@parent" />
    </xsl:variable>
    <xsl:variable name="tableName" select="env:TableName($back-role, local-name(..))" />
    <xsl:variable name="test" select="starts-with($tableName, local-name(..))" />
    <!--multiplicity <xsl:value-of select="@multiplicity"/> <xsl:value-of select="@adjacentMultiplicity"/>-->
    <xsl:choose>
      <xsl:when test="@adjacentMultiplicity = '1'">
        <xsl:if test="boolean($test) or @navigable = 'false'">
          ,`<xsl:value-of select="local-name(..)"/>` char(36)</xsl:if>
      </xsl:when>
      <xsl:otherwise>
        ,`<xsl:value-of select="local-name(..)"/>` char(36)</xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="typing:reference[@multiplicity = '1']" mode="indexcolumn">
    <xsl:param name="classId"/>
    <!--multiplicity <xsl:value-of select="@multiplicity"/> <xsl:value-of select="@adjacentMultiplicity"/>-->
    <xsl:variable name="back-role">
      <xsl:value-of select="@parent" />
    </xsl:variable>
    <xsl:variable name="table">
      <xsl:value-of select="local-name(../..)"/>
      <!--<xsl:choose>
				<xsl:when test="@parent = local-name(../..)"><xsl:value-of select="@parent"/></xsl:when>
				<xsl:when test="../../typing:entity/@parentEntity"><xsl:value-of select="../../typing:entity/@parentEntity"/></xsl:when>
				<xsl:otherwise><xsl:value-of select="local-name(../..)"/></xsl:otherwise>
			</xsl:choose>-->
    </xsl:variable>
    <xsl:variable name="tableName" select="env:TableName($back-role, local-name(..))" />
    <xsl:variable name="test" select="starts-with($tableName, local-name(..))" />
    <xsl:choose>
      <xsl:when test="@adjacentMultiplicity = '1'">
        <xsl:if test="boolean($test) or @navigable = 'false'">
        alter table `<xsl:value-of select="$table"/>` add INDEX `<xsl:value-of select="local-name(..)"/>` (`<xsl:value-of select="local-name(..)"/>`);
        </xsl:if>
      </xsl:when>
      <xsl:otherwise>
        alter table `<xsl:value-of select="$table"/>` add INDEX `<xsl:value-of select="local-name(..)"/>` (`<xsl:value-of select="local-name(..)"/>`);
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="typing:reference[@multiplicity = '*' and not(@inherited='true')]" mode="create">

    <xsl:choose>
      <!--<xsl:when test="count(../../typing:entity/@parent) = 1 or @adjacentMultiplicity = '*'" >-->
      <xsl:when test="@navigable = 'false' or @adjacentMultiplicity = '*'">
        <xsl:variable name="back-role">
          <xsl:choose>
            <xsl:when test="@parent">
              <xsl:value-of select="@parent" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="local-name(../..)" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name="tableName" select="env:TableName($back-role, local-name(..))" xmlns:env="urn:xmd-environment" />
        CREATE TABLE if not exists `<xsl:value-of select="$tableName"/>` (
        `<xsl:value-of select="$back-role"/>` char(36),
        `<xsl:value-of select="local-name(..)"/>` char(36)
        ,`modifiele` datetime not null default '1800-01-01T00:00:00'
        ,`modifiepar` varchar(30) not null default ''
        ,PRIMARY KEY (`<xsl:value-of select="$back-role"/>`, `<xsl:value-of select="local-name(..)"/>`)
        );
        
      </xsl:when>
      <xsl:otherwise></xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="typing:reference[@multiplicity = '*' and not(@inherited='true')]" mode="indexcolumn">
    <!-- multiplicity <xsl:value-of select="@multiplicity"/> <xsl:value-of select="@adjacentMultiplicity"/>-->
    <xsl:choose>
      <!--<xsl:when test="count(../../typing:entity/@parent) = 1 or @adjacentMultiplicity = '*'" >-->
      <xsl:when test="@navigable = 'false' or @adjacentMultiplicity = '*'">
        <xsl:variable name="back-role">
          <xsl:choose>
            <xsl:when test="@parent">
              <xsl:value-of select="@parent" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="local-name(../..)" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name="tableName" select="env:TableName($back-role, local-name(..))" xmlns:env="urn:xmd-environment" />
        alter table `<xsl:value-of select="$tableName"/>` add INDEX `<xsl:value-of select="local-name(..)"/>` (`<xsl:value-of select="local-name(..)"/>`);
        alter table `<xsl:value-of select="$tableName"/>` add INDEX `<xsl:value-of select="$back-role"/>` (`<xsl:value-of select="$back-role"/>`);        
      </xsl:when>
      <xsl:otherwise></xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>